/*
 * jsQuick JavaScript Library v1.1
 * 
 * Copyright 2011, mr.P
 * 
 * Date: 01.09.2011 17:15:01
 */
(function (window, undefined) {
	
  var document	= window.document,
		location		= window.location,
		navigator	= window.navigator;
  
	var jsQuick = (function () {
		// локальная копия
		var jsQuick = function(query) {
			return new jsQuick.core.construct(query);
		},
		// основные р. выражения
		rExpr = /^(#([\w\-]+)$)/,
		// правый/левый trim
		trimLeft  = /^\s+/, 
		trimRight = /\s+$/,
    
		toString = Object.prototype.toString;
    
		jsQuick.core = jsQuick.prototype = {
			construct: function(query) {
				var elem, match;
				// $(DOM элемент)
				if (query.nodeType) {
					this.context = this[0] = query;
					this.length  = 1;
				} // $(body)	
				else if (query == "body" && document.body) {
					this.context = document;
					this[0] = document.body;
					this.query   = query;
					this.length  = 1;
				} // $(#id)
				else if (typeof query === "string") {
					match = rExpr.exec(query);
					if (match) {
						elem = document.getElementById(match[2]);
						if (elem) {
							this.length = 1;
							this[0] = elem;
						}
						this.context = document;
						return this;
					}
				}
			},
			obj: function() {return this[0];},
			length: 0,
			// свойства
			html: function(value) {
				if (!value) {return this[0].innerHTML;}
				else {this[0].innerHTML = value;}
			},
			className: function(value) {
				if (!value) {return this[0].className;}
				else {this[0].className = value;}
			},
			value: function(value) {
				if (!value) {return this[0].value;}
				else {this[0].value = value;}
			},
			src: function(value) {
				if (!value) {return this[0].src;}
				else {this[0].src = value;}
			},
			href: function(value) {
				if (!value) {return this[0].value;}
				else {this[0].value = value;}
			},
			disabled: function(value) {
				if (!value && typeof value != "boolean") {return this[0].disabled;}
				else {this[0].disabled = value;}
			},
			checked: function(value) {
				if (!value && typeof value != "boolean") {return this[0].checked;}
				else {this[0].checked = value;}
			},
			// css
			css: function(name, value) {
				style = cssNameValid(name);
				this[0].style[style] = value; 
			},
			height: function (value) {this[0].style.height = value;},
			width: function (value) {this[0].style.width = value;},
			display: function(value) {this[0].style.display = value;},
			show: function () {this.display("block");},
			hide: function () {this.display("none");},
			// события
			click: function(handler) {this[0].onclick = handler;},
      mouseover: function(handler) {this[0].onmouseover = handler;},
      mouseout:  function(handler) {this[0].onmouseout = handler;},
      mousedown: function(handler) {this[0].onmousedown = handler;},
      mouseup:   function(handler) {this[0].onmouseup = handler;},
      keydown: function(handler) {this[0].onkeydown = handler;},
      // свойства
      isShow: function() {
        if (this[0].style.display == "none") {return false;}
        else {return true;}
      }
    };
    jsQuick.core.construct.prototype = jsQuick.core;
    
    return jsQuick;
  })();
  
  var rdash = /-([a-z])/ig,
  a_show,
  a_hide,
  r_up,
  r_down;
  
	// функции jsQuick
	// AJAX
	jsQuick.get = function(url, data, fun_callback) {jsQuick.extend.ajax("GET", url, data, fun_callback, true)};
	jsQuick.post = function(url, data, fun_callback) {jsQuick.extend.ajax("POST", url, data, fun_callback, true)};
	jsQuick.ajax = function(type, url, data, fun_callback, synch) {jsQuick.extend.ajax(type, url, data, fun_callback, synch)};
	jsQuick.Interpretation = function(text) {return reqInterpretation(text)};
	jsQuick.ready = function(handler) {
		if(document.addEventListener) {   // Mozilla, Opera, Webkit are all happy with this
			document.addEventListener("DOMContentLoaded", function() {
				document.removeEventListener( "DOMContentLoaded", arguments.callee, false);
				handler();
			}, false);
		} else if(document.attachEvent) {   // IE is different...
			document.attachEvent("onreadystatechange", function() {
				if(document.readyState === "complete") {
					document.detachEvent("onreadystatechange", arguments.callee);
					handler();
				}
			});
		}
	}
  
  // расширения jsQuick
  jsQuick.extend = {
    ajax: function(type, url, data, fun_callback, synch) {
      var xhr;
      // создание объекта
      if (window.XMLHttpRequest) {xhr = new XMLHttpRequest();}
      else if (window.ActiveXObject) {
        try {xhr = new ActiveXObject("Msxml2.XMLHTTP");} 
        catch (e) {}                                   
        try {xhr = new ActiveXObject("Microsoft.XMLHTTP");} 
        catch (e) {}
      }
      // обработчик
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
          xhr.onreadystatechange = null;
          var request = reqInterpretation(xhr.responseText);
          return fun_callback(request);
        }
      }
      // отправка запроса
      if (type == "GET") {
        xhr.open("GET", url+'?'+data, synch);
        xhr.send(null);  
      } else {
        xhr.open("POST", url, synch);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded charset=windows-1251");
        xhr.send(data);
      }
    }
  }

  function reqInterpretation(text) {
    var RezObj  = new Object(),
        Value   = "",
        Option  = ""
        bOption = false;
    // обработка текста
    for (var iCh = 0; iCh < text.length; iCh++) {
      if (!bOption) {
        if (text[iCh] == '{') {Option = "";}
        else {
          if (text[iCh] == '=' && text[iCh-1] == '}') {bOption = true;}
          else if (text[iCh] != '}') {Option += text[iCh];}
        }
      } else {
        if (text[iCh] == '[') {Value = "";}
        else {
          if (text[iCh] == ';' && text[iCh-1] == ']') {
            bOption = false;
            switch (Value) {
              case "_":     {Value = null; break;}
              case "true":  {Value = true; break;}
              case "false": {Value = false;break;}
              default: {if (Value*1) {Value *= 1;} break;}
            }
            RezObj[Option] = Value;
          } else if (text[iCh] != ']') Value += text[iCh];
        }
      }
    }
    return RezObj;
  }  
  
  function cssNameValidR(all, letter) {
    return letter.toUpperCase();
  }
  function cssNameValid(string) {
    return string.replace(rdash, cssNameValidR);
  }
  window.jsQuick = window.$ = jsQuick;
})(window)